package com.lyz.myself.controller;

import com.github.pagehelper.PageInfo;
import com.lyz.myself.config.utils.Result;
import com.lyz.myself.config.utils.base.CodeMsg;
import com.lyz.myself.pojo.Po.Business;
import com.lyz.myself.pojo.Vo.BusinessProductVo;
import com.lyz.myself.service.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: myself
 * @Date: 2019/1/26 10:58
 * @Author: Mr.Deng
 * @Description:
 */
@Api(description = " 商机相关信息")
@RestController
@RequestMapping("/businessController")
public class BusinessController{

    @Autowired
    private BusinessService businessService;

    @ApiOperation("添加商机")
    @RequiresAuthentication
    @RequestMapping(path = "/insertBusiness", method = RequestMethod.POST)
    public Result insertBusiness(@RequestBody BusinessProductVo list) {
        if (list.getBusiness() != null) {
            boolean business_id = businessService.insertBusiness(list);
            if (business_id ==true) {
                return Result.success("添加成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "添加失败！");
            }
        } else {
            return Result.error(CodeMsg.ERROR, "请输入要添加的信息！");
        }
    }

    @ApiOperation("查询我负责的所有商机")
    @RequiresAuthentication
    @RequestMapping(path = "/selectBusiness", method = RequestMethod.GET)
    public Result selectBusiness(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        PageInfo<Map<String, Object>> business_id = businessService.selectBusiness(map);
            if (business_id.getList().size()>0) {
                return Result.success(business_id,"查询成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "暂无信息！");
            }
    }

    @ApiOperation("根据id查看商机信息，待完善，没写完，差商品和附件")
    @RequiresAuthentication
    @RequestMapping(path = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Result selectByPrimaryKey(@RequestParam("business_id") Integer business_id) {
        List<Map<String, String>> businessId = businessService.selectByPrimaryKey(business_id);
        if (businessId!=null) {
            return Result.success(businessId,"查询成功！");
        } else {
            return Result.error(CodeMsg.ERROR, "暂无信息！");
        }
    }
}
